<?php

/**
 * This is the model class for table "fitz_service".
 *
 * The followings are the available columns in table 'fitz_service':
 * @property integer $id
 * @property string $date
 * @property string $purchaseDate
 * @property integer $technicianId
 * @property string $firstName
 * @property string $LastName
 * @property string $address
 * @property string $suburb
 * @property string $postcode
 * @property string $state
 * @property string $homePhone
 * @property string $workPhone
 * @property string $mobile
 * @property string $email
 * @property integer $stairs
 * @property integer $productId
 * @property string $serialNo
 * @property string $generalComment
 * @property string $partComment
 * @property integer $warranty
 * @property string $faultDescription
 * @property string $customerAccountNo
 * @property string $storeTinNo
 * @property string $otherInstruction
 * @property string $dateCommenced
 * @property string $dateCompleted
 * @property integer $chargeApplicable
 * @property string $customerCharge
 * @property integer $fundsRecovered
 * @property integer $paymentType
 * @property integer $technicainPaid
 * @property string $technicainPaidAmount
 * @property integer $workCarriedOut
 * @property string $note
 * @property integer $storeId
 * @property string $emailNote
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property PartAction[] $partActions
 * @property Store $store
 * @property Installer $technician
 * @property Product $product
 * @property ServicePayment $servicePayment
 */
class Service extends CActiveRecord
{
	const STATUS_NEW=0;
	const STATUS_PROCESSING=1;
	const STATUS_JOB_ALLOCATED=2;
	const STATUS_FINISHED=10;

	public static function getStatusArray($status=null){
		$statusArray= array(
		self::STATUS_NEW=>'New',
		self::STATUS_PROCESSING=>'In Progress',
		self::STATUS_JOB_ALLOCATED=>'Job Allocated',
		self::STATUS_FINISHED=>'Finished'
		);
		if($status!==null){
			return $statusArray[$status];
		}
		return $statusArray;
	}
	public static function getActionUrlFor($service){
		switch($service->status){
			case self::STATUS_NEW:
				return CHtml::normalizeUrl(array("service/startProcess","id"=>$service->id));
			case self::STATUS_PROCESSING:
				return CHtml::normalizeUrl(array("service/allocateJob","id"=>$service->id));
			case self::STATUS_JOB_ALLOCATED:
				return CHtml::normalizeUrl(array("service/finishJob","id"=>$service->id));
			case self::STATUS_FINISHED:
				return '';
		}
	}
	public static function getActionLabelFor($service){
		switch($service->status){
			case self::STATUS_NEW:
				return 'Process';
			case self::STATUS_PROCESSING:
				return 'Allocate Job';
			case self::STATUS_JOB_ALLOCATED:
				return 'Finish Job';
			case self::STATUS_FINISHED:
				return '';
		}
	}

	/**
	 * Returns the static model of the specified AR class.
	 * @return Service the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'fitz_service';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
		//array('date, purchaseDate, firstName, LastName, address, suburb, postcode, state, homePhone, workPhone, mobile, email, stairs, productId, warranty, storeTinNo, chargeApplicable, fundsRecovered, paymentType, technicainPaid, workCarriedOut, storeId, status', 'required'),
		array('purchaseDate, firstName, LastName, productId, warranty, storeId', 'required'),
		array('technicianId, stairs, productId, warranty, chargeApplicable, fundsRecovered, paymentType, technicainPaid, workCarriedOut, storeId, status', 'numerical', 'integerOnly'=>true),
		array('firstName, LastName, suburb, customerAccountNo, storeTinNo', 'length', 'max'=>100),
		array('address, email, serialNo', 'length', 'max'=>255),
		array('postcode, state, homePhone, workPhone, mobile', 'length', 'max'=>45),
		array('customerCharge, technicainPaidAmount', 'length', 'max'=>8),
		array('generalComment, partComment, faultDescription, otherInstruction, dateCommenced, dateCompleted, note, emailNote', 'safe'),

		array('fundsRecovered,technicainPaid,workCarriedOut',
			'default','value'=>'0','setOnEmpty'=>true,'on'=>'insert,update'),
		array('customerCharge,technicainPaidAmount',
			'default','value'=>null,'setOnEmpty'=>true,'on'=>'insert,update'),
		array('address, suburb, postcode, state, homePhone, workPhone, mobile, email, storeTinNo',
			'default','value'=>'','setOnEmpty'=>true,'on'=>'insert,update'),

		array('mobile', 'requireContactNo','on'=>'insert,update'),
		
		// The following rule is used by search().
		// Please remove those attributes that should not be searched.
		array('id, date, purchaseDate, technicianId, firstName, LastName, address, suburb, postcode, state, homePhone, workPhone, mobile, email, stairs, productId, serialNo, generalComment, partComment, warranty, faultDescription, customerAccountNo, storeTinNo, otherInstruction, dateCommenced, dateCompleted, chargeApplicable, customerCharge, fundsRecovered, paymentType, technicainPaid, technicainPaidAmount, workCarriedOut, note, storeId, emailNote, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'partActions' => array(self::HAS_MANY, 'PartAction', 'serviceId'),
			'store' => array(self::BELONGS_TO, 'Store', 'storeId'),
			'technician' => array(self::BELONGS_TO, 'Installer', 'technicianId'),
			'product' => array(self::BELONGS_TO, 'Product','productId'),
			'servicePayment' => array(self::BELONGS_TO, 'ServicePayment','paymentType'),
		);
	}
	
	public function behaviors(){
		return array(
  		'CTimestampBehavior' => array(
  			'class' => 'zii.behaviors.CTimestampBehavior',
  			'createAttribute' => 'date',
			'updateAttribute' => null,
		)
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'date' => 'Request Date',
			'purchaseDate' => 'Purchase Date',
			'technicianId' => 'Technician',
			'firstName' => 'First Name',
			'LastName' => 'Last Name',
			'address' => 'Address',
			'suburb' => 'Suburb',
			'postcode' => 'Postcode',
			'state' => 'State',
			'homePhone' => 'Home Phone',
			'workPhone' => 'Work Phone',
			'mobile' => 'Mobile',
			'email' => 'Email',
			'stairs' => 'Stairs',
			'productId' => 'Product',
			'serialNo' => 'Serial No',
			'generalComment' => 'General Comment',
			'partComment' => 'Part Comment',
			'warranty' => 'Warranty',
			'faultDescription' => 'Fault Description',
			'customerAccountNo' => 'Customer A/C No',
			'storeTinNo' => 'Store TIN No',
			'otherInstruction' => 'Other Instruction',
			'dateCommenced' => 'Date Commenced',
			'dateCompleted' => 'Date Completed',
			'chargeApplicable' => 'Charge Applicable',
			'customerCharge' => 'Customer Charge',
			'fundsRecovered' => 'Funds Recovered',
			'paymentType' => 'Payment Type',
			'technicainPaid' => 'Technicain Paid',
			'technicainPaidAmount' => 'Technicain Paid Amount',
			'workCarriedOut' => 'Work Carried Out',
			'note' => 'Note',
			'storeId' => 'Store',
			'emailNote' => 'Email Note',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('date',$this->date,true);
		$criteria->compare('purchaseDate',$this->purchaseDate,true);
		$criteria->compare('technicianId',$this->technicianId);
		$criteria->compare('firstName',$this->firstName,true);
		$criteria->compare('LastName',$this->LastName,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('suburb',$this->suburb,true);
		$criteria->compare('postcode',$this->postcode,true);
		$criteria->compare('state',$this->state,true);
		$criteria->compare('homePhone',$this->homePhone,true);
		$criteria->compare('workPhone',$this->workPhone,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('stairs',$this->stairs);
		$criteria->compare('productId',$this->productId);
		$criteria->compare('serialNo',$this->serialNo,true);
		$criteria->compare('generalComment',$this->generalComment,true);
		$criteria->compare('partComment',$this->partComment,true);
		$criteria->compare('warranty',$this->warranty);
		$criteria->compare('faultDescription',$this->faultDescription,true);
		$criteria->compare('customerAccountNo',$this->customerAccountNo,true);
		$criteria->compare('storeTinNo',$this->storeTinNo,true);
		$criteria->compare('otherInstruction',$this->otherInstruction,true);
		$criteria->compare('dateCommenced',$this->dateCommenced,true);
		$criteria->compare('dateCompleted',$this->dateCompleted,true);
		$criteria->compare('chargeApplicable',$this->chargeApplicable);
		$criteria->compare('customerCharge',$this->customerCharge,true);
		$criteria->compare('fundsRecovered',$this->fundsRecovered);
		$criteria->compare('paymentType',$this->paymentType);
		$criteria->compare('technicainPaid',$this->technicainPaid);
		$criteria->compare('technicainPaidAmount',$this->technicainPaidAmount,true);
		$criteria->compare('workCarriedOut',$this->workCarriedOut);
		$criteria->compare('note',$this->note,true);
		$criteria->compare('storeId',$this->storeId);
		$criteria->compare('emailNote',$this->emailNote,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
			'pagination'=>array('pageSize'=>50),
		));
	}
	
	/**
	 * 
	 * Validate Homephone, workphone, mobile, make sure at least one contact number is input.
	 * @param unknown_type $attribute
	 * @param unknown_type $params
	 */	
    public function requireContactNo($attribute,$params)
    {
        $notNull = 0;
        if(!empty($this->homePhone)){
        	$notNull++;
        }
    	if(!empty($this->workPhone)){
        	$notNull++;
        }
    	if(!empty($this->mobile)){
        	$notNull++;
        }
        
        if($notNull == 0){
        	$this->addError('homePhone','Home Phone is empty! At least ONE contact number should be input.');
        	$this->addError('workPhone','Work Phone is empty! At least ONE contact number should be input.');
        	$this->addError('mobile','Mobile is empty! At least ONE contact number should be input.');
        	
        }
    }
}